/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgMapPageModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`page_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '页ID',
  //`map_id` bigint(20) unsigned NOT NULL COMMENT '分页映射ID',
  //`page_index` int(3) NOT NULL DEFAULT '0' COMMENT '页下标（0开始）',
  //`backup_flg` int(1) NOT NULL DEFAULT '0' COMMENT '0普通页; 1保留页',
  //`data` mediumtext COMMENT '页内容',
  //`hash_tag` varchar(100) DEFAULT '0' COMMENT '标记',
  //`thumb` text COMMENT '缩略图',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',


};
TgMapPageModel.prototype = new ItbBaseModel();

// 取得表名
TgMapPageModel.prototype.getTableName = function(){
    return 'tg_map_page';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgMapPageModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'page_id='+this.page_id;
}


// 取得排序方法（需要各派生类进行重载）
TgMapPageModel.prototype.getOrderBy = function(){
    return ' page_index asc ';
}

//------------------------------------------------------------------
/* 获取分页page信息
 * 参数:map_id/page_id
 * 参数:callback:回调
 * */
TgMapPageModel.prototype.getMapPageInfoById = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' select t.page_id, t.map_id, t.page_index, t.backup_flg, '+
            ' t.data, t.hash_tag, t.thumb '+
            ' from TG_MAP_PAGE t'+
            ' where t.del_flg = 0 ';
        if(THIS.map_id){
            sql += ' and t.map_id = '+ THIS.map_id;
        }
        if(THIS.page_id){
            sql += ' and t.page_id = '+ THIS.page_id;
        }
        sql += ' order by t.map_id, t.page_index ';

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();

            callback(error, result);
        });
    });
}
//--------------------------------end-------------------------------


// 导出
module.exports = TgMapPageModel;